package com.horoscope.astrology.zodiac.signs.free.adapter

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Context
import android.graphics.Paint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.horoscope.astrology.zodiac.signs.free.MmoConstant
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.adapter.recyclerHolder.ShareViewHolder
import com.horoscope.astrology.zodiac.signs.free.bean.ContentBean
import com.horoscope.astrology.zodiac.signs.free.bean.ZodiacResultDataBean
import com.horoscope.astrology.zodiac.signs.free.utils.ConstellationUtil
import com.horoscope.astrology.zodiac.signs.free.utils.ShareManager
import com.horoscope.astrology.zodiac.signs.free.view.CircleBarView

class ZodiacResultAdapter(private val mContext: Context) :
    RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private val mDataList = ArrayList<ContentBean>()
    private var mBean = ZodiacResultDataBean()
    private var first = true

    fun setData(list: ArrayList<ContentBean>) {
        mDataList.clear()
        mDataList.add(0, ContentBean())
        mDataList.addAll(list)
        mDataList.add(ContentBean())//share
        notifyDataSetChanged()

    }

    fun setHeadBean(bean: ZodiacResultDataBean) {
        mBean = bean
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        p1: Int
    ): RecyclerView.ViewHolder {
        return when (p1) {
            SHARE -> {
                val view = LayoutInflater.from(mContext).inflate(R.layout.item_share, parent, false)
                return ShareViewHolder(view)
            }
            TYPE_HEAD -> {
                val view = LayoutInflater.from(mContext)
                    .inflate(R.layout.item_header_zodiac_result, parent, false)
                HeadViewHolder(view)
            }
            else -> {
                val view = LayoutInflater.from(mContext)
                    .inflate(R.layout.item_zodiac_result, parent, false)
                ContentViewHolder(view)
            }
        }

    }

    override fun getItemCount(): Int {
        return mDataList.size
    }


    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(
        holder: RecyclerView.ViewHolder,
        position: Int
    ) {
        when (holder) {
            is ContentViewHolder -> {
                val bean = mDataList[position]
                holder.content.text = bean.content
                holder.name.text = bean.title
                when(bean.title){
                    "Summary" -> holder.percent.setBackgroundResource(R.drawable.shape_personal)
                    "Relationship" -> holder.percent.setBackgroundResource(R.drawable.shape_love)
                    "Values" -> holder.percent.setBackgroundResource(R.drawable.shape_health)
                    "Trust" -> holder.percent.setBackgroundResource(R.drawable.shape_career)
                }
                holder.percent.text = bean.rate.toString() + "%"
            }
            is HeadViewHolder -> {
                holder.h1.text = mBean.horoscope1
                holder.h2.text = mBean.horoscope2
                holder.icon1.setImageResource(ConstellationUtil.getIconByName(mBean.horoscope1))
                holder.icon2.setImageResource(ConstellationUtil.getIconByName(mBean.horoscope2))
                val time = getAnimTime()
                holder.barView.setProgressNum(mBean.rate.toFloat(), time)
                holder.barView.setTextView(holder.percent)
                holder.barView.setOnAnimationListener(object : CircleBarView.OnAnimationListener {
                    override fun howTiChangeProgressColor(
                        paint: Paint?,
                        interpolatedTime: Float,
                        updateNum: Float,
                        maxNum: Float
                    ) {
                    }

                    override fun howToChangeText(
                        interpolatedTime: Float,
                        updateNum: Float,
                        maxNum: Float
                    ): String {
                        val percent = (interpolatedTime * updateNum / maxNum * 100).toInt()
                        return "$percent%"
                    }


                })
            }
            is ShareViewHolder -> {
                holder.facebook.setOnClickListener {
                    ShareManager.shareToFacebook(
                        holder.itemView.context as Activity,
                        MmoConstant.URL_RATING_HTTP
                    )
                }
                holder.more.setOnClickListener {
                    ShareManager.sysCallsShare(
                        holder.itemView.context as Activity,
                        MmoConstant.URL_RATING_HTTP
                    )
                }
            }
        }
    }

    private fun getAnimTime(): Int {
        val time: Int
        if (first) {
            time = 500
            first = false
        } else {
            time = 0
        }
        return time
    }

    override fun getItemViewType(position: Int): Int {
        return when (position) {
            0 -> {
                TYPE_HEAD
            }
            mDataList.size - 1 -> {
                SHARE
            }
            else -> {
                TYPE_START
            }
        }
    }

    companion object {
        const val TYPE_HEAD = 0x11
        const val TYPE_START = 0x12
        const val SHARE = 0x13
    }

    open inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)

    inner class ContentViewHolder(itemView: View) : MyViewHolder(itemView) {
        val content = itemView.findViewById<TextView>(R.id.text_view_content)!!
        val name = itemView.findViewById<TextView>(R.id.text_view_zodiac_name)!!
        val percent = itemView.findViewById<TextView>(R.id.text_view_zodiac_percent)!!

    }

    inner class HeadViewHolder(itemView: View) : MyViewHolder(itemView) {
        val barView = itemView.findViewById<CircleBarView>(R.id.circle_bar_view)!!
        val percent = itemView.findViewById<TextView>(R.id.text_view_circle_percent)!!
        val h1 = itemView.findViewById<TextView>(R.id.text_view_h1)!!
        val h2 = itemView.findViewById<TextView>(R.id.text_view_h2)!!
        val icon1 = itemView.findViewById<ImageView>(R.id.image_view_h1)!!
        val icon2 = itemView.findViewById<ImageView>(R.id.image_view_h2)!!
    }

}